﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EventScavenger
{
    public partial class Collectors : Form
    {
        public Collectors()
        {
            InitializeComponent();
        }

        private EventScavengerDAL eventScavengerDAL;

        public DialogResult Show(EventScavengerDAL eventScavengerDAL)
        {
            this.eventScavengerDAL = eventScavengerDAL;
            return ShowDialog();
        }

        #region Form events
        private void Collectors_Load(object sender, EventArgs e)
        {
            lvwCollectors.Items.Add("Loading...");
            backgroundWorkerLoading.RunWorkerAsync();
        } 
        #endregion

        #region Loading
        private void backgroundWorkerLoading_DoWork(object sender, DoWorkEventArgs e)
        {
            System.Threading.Thread.Sleep(500);
            this.Invoke((MethodInvoker)delegate()
            {
                List<Collector> collectors = eventScavengerDAL.GetCollectorList();
                Application.DoEvents();
                lvwCollectors.Items.Clear();
                foreach (Collector collector in collectors)
                {
                    ListViewItem lvi = new ListViewItem(collector.Name);
                    lvi.SubItems.Add(collector.ThreadRecycleFrequencyMinutes.ToString());
                    lvi.SubItems.Add(collector.NextRecycleTime.ToString("yyyy-MM-dd HH:mm:ss"));
                    lvi.SubItems.Add(collector.ThreadRecyclingEnabled.ToString());
                    lvi.Tag = collector;
                    lvwCollectors.Items.Add(lvi);
                }
            });
        } 
        #endregion

        #region Toolbar events
        private void toolStripButtonNew_Click(object sender, EventArgs e)
        {
            try
            {
                CollectorEdit collectorEdit = new CollectorEdit();
                collectorEdit.SelectedCollector.ThreadRecyclingEnabled = true;
                if (collectorEdit.ShowDialog() == DialogResult.OK)
                {
                    eventScavengerDAL.InsertCollector(collectorEdit.SelectedCollector);
                    backgroundWorkerLoading.RunWorkerAsync();
                    //ListViewItem lvi = new ListViewItem(collectorEdit.SelectedCollector.Name);
                    //lvi.SubItems.Add(collectorEdit.SelectedCollector.ThreadRecycleFrequencyMinutes.ToString());
                    //lvi.SubItems.Add(collectorEdit.SelectedCollector.NextRecycleTime.ToString("yyyy-MM-dd HH:mm:ss"));
                    //lvi.SubItems.Add(collectorEdit.SelectedCollector.ThreadRecyclingEnabled.ToString());
                    //lvi.Tag = collectorEdit.SelectedCollector;
                    //lvwCollectors.Items.Add(lvi);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Edit", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void toolStripButtonEdit_Click(object sender, EventArgs e)
        {
            if (lvwCollectors.SelectedItems.Count > 0)
            {
                try
                {
                    CollectorEdit collectorEdit = new CollectorEdit();
                    collectorEdit.SelectedCollector = (Collector)lvwCollectors.SelectedItems[0].Tag;
                    if (collectorEdit.ShowDialog() == DialogResult.OK)
                    {
                        eventScavengerDAL.UpdateCollector(collectorEdit.SelectedCollector);
                        lvwCollectors.SelectedItems[0].Tag = collectorEdit.SelectedCollector;
                        lvwCollectors.SelectedItems[0].Text = collectorEdit.SelectedCollector.Name;
                        lvwCollectors.SelectedItems[0].SubItems[1].Text = collectorEdit.SelectedCollector.ThreadRecycleFrequencyMinutes.ToString();
                        lvwCollectors.SelectedItems[0].SubItems[2].Text = collectorEdit.SelectedCollector.NextRecycleTime.ToString("yyyy-MM-dd HH:mm:ss");
                        lvwCollectors.SelectedItems[0].SubItems[3].Text = collectorEdit.SelectedCollector.ThreadRecyclingEnabled.ToString();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Edit", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        private void toolStripButtonRemove_Click(object sender, EventArgs e)
        {
            try
            {
                if (lvwCollectors.SelectedItems.Count > 0)
                {
                    if (MessageBox.Show("Are you sure you want to delete this collector(s)?\r\nIf there are any logs still linked with it the delete will fail", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        foreach (ListViewItem lvi in lvwCollectors.SelectedItems)
                        {
                            Collector collector = (Collector)lvi.Tag;
                            eventScavengerDAL.DeleteCollector(collector);
                        }
                    }
                    backgroundWorkerLoading.RunWorkerAsync();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Delete", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        } 
        #endregion

        #region Listview events
        private void lvwCollectors_SelectedIndexChanged(object sender, EventArgs e)
        {
            toolStripButtonEdit.Enabled = lvwCollectors.SelectedItems.Count > 0;
            editToolStripMenuItem.Enabled = lvwCollectors.SelectedItems.Count > 0;
            toolStripButtonRemove.Enabled = (lvwCollectors.SelectedItems.Count > 0);
            deleteToolStripMenuItem.Enabled = (lvwCollectors.SelectedItems.Count > 0);
        }
        private void lvwCollectors_Resize(object sender, EventArgs e)
        {
            columnHeaderCollector.Width = lvwCollectors.ClientSize.Width - lvwCollectors.Columns[1].Width - lvwCollectors.Columns[2].Width - lvwCollectors.Columns[3].Width;
        } 
        private void lvwCollectors_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                toolStripButtonEdit_Click(sender, e);
            }
        }
        #endregion
   
    }
}
